<?php

namespace app\forms\item;

use yii\db\Exception;
use app\forms\Model;
use app\models\Item;
use yii\web\HttpException;
use app\validators\PositiveInteger;

/**
 * 权限项 首页
 */
class IndexForm extends Model
{
    public $parentId;

    public function rules()
    {
        return [['parentId', PositiveInteger::class]];
    }

    /**
     * @param $param
     * @return array
     * @throws HttpException
     */
    public static function index($param)
    {
        $form = new self();
        $form->setAttributes($param);
        $valid = $form->validate();

        try {
            $model = new Item;
            $item = $model->getTree($valid ? $form['parentId'] : 0);
            $itemTop = $model->getTop();
            return ['item' => $item, 'itemTop' => $itemTop];
        } catch (Exception $e) {
            $form->serverError();
        }
    }

}